﻿

namespace Forest.Lion.Data.SqlServer {
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    using Forest.Lion.Model;

    public class LionDbContext : DbContext, IUnitOfWork {
        public DbSet<Company> Companys { get; set; }
        public DbSet<JobPosition> JobPositions { get; set; }
        public DbSet<Lov> Lovs { get; set; }
        public DbSet<Department> Departments { get; set; }
        public DbSet<Contract> Contracts { get; set; }
        public DbSet<MessageTemplate> MessageTemplates { get; set; }
        public DbSet<Deal> Deals { get; set; }
        public DbSet<Work> Works { get; set; }
        public DbSet<Training> Trainings { get; set; }
        public DbSet<Skill> Skills { get; set; }
        public DbSet<Education> Educations { get; set; }
        public DbSet<Receipt> Receipts { get; set; }
        public DbSet<Certification> Certifications { get; set; }
        public DbSet<Interview> Interviews { get; set; }
        public DbSet<Resume> Resumes { get; set; }
        public DbSet<Tag> Tags { get; set; }
        public DbSet<Candidate> Candidates { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder) {
            SetupCompanyEntity(modelBuilder);
            SetupJobPositionEntity(modelBuilder);
            SetupLovEntity(modelBuilder);
            SetupDepartmentEntity(modelBuilder);
            SetupContractEntity(modelBuilder);
            SetupMessageTemplateEntity(modelBuilder);
            SetupDealEntity(modelBuilder);
            SetupWorkEntity(modelBuilder);
            SetupTrainingEntity(modelBuilder);
            SetupSkillEntity(modelBuilder);
            SetupEducationEntity(modelBuilder);
            SetupReceiptEntity(modelBuilder);
            SetupCertificationEntity(modelBuilder);
            SetupInterviewEntity(modelBuilder);
            SetupResumeEntity(modelBuilder);
            SetupTagEntity(modelBuilder);
            SetupCandidateEntity(modelBuilder);
        }

        private static void SetupCandidateEntity(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Candidate>().ToTable("nclion_candidate");
            modelBuilder.Entity<Candidate>().HasKey(c => c.Id);
        }

        private static void SetupTagEntity(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Tag>().ToTable("nclion_tag");
            modelBuilder.Entity<Tag>().HasKey(c => c.Id);
        }

        private static void SetupDealEntity(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Deal>().ToTable("ncp_deal");
            modelBuilder.Entity<Deal>().HasKey(c => c.Id);
        }

        private static void SetupCompanyEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<Company>().ToTable("ncp_company");
            modelBuilder.Entity<Company>().HasKey(c => c.Id);
        }

        private static void SetupJobPositionEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<JobPosition>().ToTable("ncp_jobposition");

            modelBuilder.Entity<JobPosition>().HasRequired(x => x.Department).WithMany().HasForeignKey(x => x.DepartmentId);
            modelBuilder.Entity<JobPosition>().HasKey(c => c.Id);
        }

        private static void SetupLovEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<Lov>().ToTable("sys_lov");
            modelBuilder.Entity<Lov>().HasKey(c => c.Id);
        }

        private static void SetupDepartmentEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<Department>().ToTable("ncp_department");
            modelBuilder.Entity<Department>().HasKey(c => c.Id);
        }

        private static void SetupContractEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<Contract>().ToTable("ncp_contract");
            modelBuilder.Entity<Contract>().HasKey(c => c.Id);
        }

        private static void SetupMessageTemplateEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<MessageTemplate>().ToTable("ncp_messagetemplate");
            modelBuilder.Entity<MessageTemplate>().HasKey(c => c.Id);
        }

        private static void SetupResumeEntity(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Resume>().ToTable("nclion_resume");
            modelBuilder.Entity<Resume>().HasKey(c => c.Id);
        }

        private static void SetupWorkEntity(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Work>().ToTable("nclion_work");
            modelBuilder.Entity<Work>().HasKey(c => c.Id);
        }

        private static void SetupTrainingEntity(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Training>().ToTable("nclion_training");
            modelBuilder.Entity<Training>().HasKey(c => c.Id);
        }

        private static void SetupSkillEntity(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Skill>().ToTable("nclion_skill");
            modelBuilder.Entity<Skill>().HasKey(c => c.Id);
        }

        private static void SetupEducationEntity(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Education>().ToTable("nclion_education");
            modelBuilder.Entity<Education>().HasKey(c => c.Id);
        }

        private static void SetupReceiptEntity(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Receipt>().ToTable("nclion_receipt");
            modelBuilder.Entity<Receipt>().HasKey(c => c.Id);
        }

        private static void SetupCertificationEntity(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Certification>().ToTable("nclion_certification");
            modelBuilder.Entity<Certification>().HasKey(c => c.Id);
        }

        private static void SetupInterviewEntity(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Interview>().ToTable("nclion_interview");
            modelBuilder.Entity<Interview>().HasKey(c => c.Id);
        }

        void IUnitOfWork.SaveChanges() {
            base.SaveChanges();
        }
    }
}
